package Blog;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.sun.xml.internal.ws.server.ServerRtException;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{
    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://127.0.0.1:8080/blog_system/blog_detail.html?blogId=",
                "博客详情页","测试用例"));
    }

    /*
        输入正确的账号 密码登录成功
         */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username,String password,String blog_list_url) throws InterruptedException {
        //打开博客登录页面
        webDriver.get("http://127.0.0.1:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入账号hyj
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 密码123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        // 获取url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 如果url相等 测试通过
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 页面展示的是hyj
        String cur_hyj = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        Assertions.assertEquals(username,cur_hyj);
    }
    /*
    博客列表页博客数量不为0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打开博客列表页
        webDriver.get("http://127.0.0.1:8080/blog_system/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //获取博客标题数
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //判断是否为0
        Assertions.assertNotEquals(0,title_num);
    }
    /*
    获取博客详情页url标题title
     */
    @Order(4)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String url,String cur_title,String blog_title) {
        //点击查看全文
        webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
        //跳转到博客详情页 获取url
        String blog_detail_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        if (blog_detail_url.contains(url)) {
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
        //获取当前页面title
        String title = webDriver.getTitle();
        Assertions.assertEquals(cur_title,title);
        //获取博客标题
        String blogtitle = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();
        Assertions.assertEquals(blog_title,blogtitle);
    }
    /*
    发布博客
     */
    @Order(3)
    @Test
    void BlogEdit() throws InterruptedException {
        //点击写博客
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //跳转到博客编辑页
        //通过js脚本进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"测试用例\"");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //获取当前页面url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8080/blog_system/blog_list.html",cur_url);
    }
    /*
    校验发布博客标题以及时间
     */
    @Order(5)
    @Test
    void BlogInfoCheck() {
        webDriver.get("http://127.0.0.1:8080/blog_system/blog_list.html");
        //获取博客标题
        String title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();
        //获取发布时间
        String time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        //校验博客标题
        Assertions.assertEquals("测试用例",title);
        if (time.contains("2023-08-12")) {
            System.out.println("测试通过");
        }else {
            System.out.println("当前时间是"+time);
            System.out.println("测试不通过");
        }
    }
    /*
    测试注销功能
     */
    @Order(6)
    @Test
    void LoginOut() {
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //点击注销按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //观察是否返回登陆页面
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8080/blog_system/login.html",cur_url);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //校验登录按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);
    }

}
